package advanced;

import java.util.concurrent.Semaphore;

public class Test3 {

    public static void main(String[] args) throws InterruptedException {

        // 可用资源有 4 个
        Semaphore semaphore = new Semaphore(4);
        // 连续 5个申请资源,第五个申请就会阻塞,直到有人释放资源
        semaphore.acquire();
        System.out.println("申请成功");
        semaphore.acquire();
        System.out.println("申请成功");
        semaphore.acquire();
        System.out.println("申请成功");
        semaphore.acquire();
        System.out.println("申请成功");
        // 阻塞,直到有其他线程释放资源
        semaphore.acquire();
        System.out.println("申请成功");
        // 释放资源
//        semaphore.release();
    }
}
